package com.zeng.createmd.restfulmodule.util;

import com.zeng.createmd.restfulmodule.constant.DataSaveLocation;
import com.zeng.createmd.restfulmodule.convert.FileVoConvertor;
import com.zeng.createmd.servicemodule.vo.FileVo;
import com.zeng.createmd.servicemodule.vo.ItemVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@Component
@Slf4j
public class FileUtil {
    @Value("${user.home.setting:C:\\Users\\51399\\Desktop}")
    String userHome;

    public FileVo listFile(FileVo sysFileVo) {
        if (StringUtils.isEmpty(sysFileVo.getPath())) {
            String usrHome = System.getProperty("user.home");
            log.info("user.home={}", usrHome);
            assert usrHome != null;
            File file = new File(usrHome, "Desktop");
            if (!file.exists()) {
                file = new File(userHome);
            }
            if (!file.exists()) {
                throw new RuntimeException("用户目录不存在");
            }
            File[] files = file.listFiles();
            assert files != null;

            return FileVo.builder().path(file.getAbsolutePath()).files(FileVoConvertor.toSysFileVo(files)).build();

        }
        File file = new File(sysFileVo.getPath());
        return FileVo.builder().path(file.getAbsolutePath()).files(FileVoConvertor.toSysFileVo(Objects.requireNonNull(file.listFiles()))).build();

    }

    public List<ItemVo> listItem() {
        File root = new File(DataSaveLocation.LOCATION);
        File items = new File(root, "data");
        if (!items.exists()) {
            return Collections.emptyList();
        }
        Object o = null;
        FileInputStream in = null;
        ObjectInputStream objectInputStream = null;
        try {
            in = FileUtils.openInputStream(items);
            objectInputStream = new ObjectInputStream(in);
            o = objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        if (o instanceof List) {
            return (List<ItemVo>) o;
        }
        return Collections.emptyList();
    }

    public void addItemVo(ItemVo itemVo) {

    }

}
